ARG VERSION=latest
FROM ghcr.io/browserless/base:$VERSION
LABEL org.opencontainers.image.source=https://github.com/browserless/browserless

ARG TARGETPLATFORM
ENV TARGETPLATFORM=${TARGETPLATFORM}

COPY fonts/* /usr/share/fonts/truetype/
COPY src src/

RUN echo "ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true" | debconf-set-selections &&\
  apt-get -y -qq install software-properties-common &&\
  apt-add-repository "deb https://archive.canonical.com/ubuntu $(lsb_release -sc) partner" &&\
  apt-get -y -qq --no-install-recommends install \
  fontconfig \
  fonts-freefont-ttf \
  fonts-gfs-neohellenic \
  fonts-indic \
  fonts-ipafont-gothic \
  fonts-kacst \
  fonts-liberation \
  fonts-noto-cjk \
  fonts-noto-color-emoji \
  fonts-roboto \
  fonts-thai-tlwg \
  fonts-ubuntu \
  fonts-wqy-zenhei \
  fonts-open-sans \
  libwebp-dev

# Chrome stable and Edge is only supported on non-ARM builds
# so it's installation is conditional on whether or not amd64
RUN if [ "$TARGETPLATFORM" = "linux/amd64" ]; then \
  ./node_modules/playwright-core/cli.js install --with-deps chrome msedge; \
  else \
  rm -rf ./src/routes/chrome ./src/routes/edge; \
  fi

# NOTE it's important to not use npx playwright-core here since it'll likely install
# a more recent version than we potentially have in our own package.json
RUN ./node_modules/playwright-core/cli.js install --with-deps chromium firefox webkit &&\
  npm run build &&\
  npm run build:function &&\
  npm prune production &&\
  npm run install:debugger &&\
  chown -R blessuser:blessuser $APP_DIR &&\
  fc-cache -f -v &&\
  apt-get -qq clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/fonts/truetype/noto

USER blessuser

CMD ["./scripts/start.sh"]
